#include "Explorer.hpp"

Explorer::Explorer(void)
{
	Debut = 0;
	Fin = 15;
}


Explorer::~Explorer(void)
{
}

void Explorer::ChangeDirectory(std::string _Path, std::string _Directory)
{
	unsigned int isFolder = 16384; unsigned int isFile = 32768;
	DIR* Dir = NULL; struct dirent* Ele = NULL; struct stat s;
	std::string PathFile = ""; std::string ElementName = ""; char Type = 0; int Size = 0; size_t found, found2;

	if (_Directory.compare("..") == 0)
	{
		found = _Path.find(WorkingDirectory);
		_Path = _Path.substr(0, found-1);

		found2 = _Path.find_last_of("\\");
		_Directory = _Path.substr(found2+1, found-1);

		_Path = _Path + "\\";
	}

	Dir = opendir(_Path.c_str());
	if (Dir != NULL)
	{
		ElementList.clear();

		WorkingPath = _Path;
		WorkingDirectory = _Directory;

		while((Ele = readdir(Dir)) != NULL)
		{
			if(Ele->d_name[1] != '\0')
			{
				ElementName = Ele->d_name;					// Name of the File or Directory
			
				if(Ele->d_type == isFile)
				{
					PathFile = WorkingPath + Ele->d_name;
					stat(PathFile.c_str(), &s);
					Size = s.st_size;
					Type = 1;
				}
				else
					Type = 0;
			
				if((PathFile.substr(PathFile.find_last_of(".") + 1) == "mp3" || PathFile.substr(PathFile.find_last_of(".") + 1) == "wav"
					|| PathFile.substr(PathFile.find_last_of(".") + 1) == "wma" || PathFile.substr(PathFile.find_last_of(".") + 1) == "flac")
					|| Ele->d_type != isFile)
				{
					std::string ElementNameView;

					if(ElementName.size() > 21)
						ElementNameView = ElementName.substr(0, 30);
					else
						ElementNameView = ElementName;

					Element Elmnt(ElementName, Type, Size, ElementNameView);
					
					ElementList.push_back(Elmnt);
				}
			}
		}

		closedir(Dir);
		Debut = 0;
		Fin = 15;
	}
}

std::string Explorer::GetWorkingPath()
{
	return WorkingPath;
}

void Explorer::viewList()
{
	std::list<Element>::iterator it;

	for (it = ElementList.begin() ; it != ElementList.end() ; ++it)
    {
      Element i = *it;

      std::cout << i.GetName() << std::endl;
    }
}

void Explorer::Draw(sf::RenderWindow &App)
{
	const sf::Input& Input = App.GetInput();
	std::string WorkingPathDraw;

	sf::Shape LineExplorer = sf::Shape::Line(sf::Vector2f(220, 27), sf::Vector2f(220, 768), 1, sf::Color(64, 64, 64));

	sf::String CurrentDirectory;
	CurrentDirectory.SetFont(TrebuchetMS);
	CurrentDirectory.SetSize(16);
	CurrentDirectory.SetPosition(20, 40);

	if(WorkingPath.size() > 21)
		WorkingPathDraw = WorkingPath.substr(WorkingPath.size()-21, WorkingPath.size());
	else
		WorkingPathDraw = WorkingPath;

	CurrentDirectory.SetText(WorkingPathDraw);

	App.Draw(CurrentDirectory);


	int Nb=0;
	int iNb=0;
	std::list<Element>::iterator it;
	for (it = ElementList.begin() ; it != ElementList.end(); ++it, Nb++)
    {
		if(Nb >= Debut && Nb < Fin)
		{
			Element i = *it;

			i.ElementText.SetPosition(20, 60+(iNb*20));

			if(		( Input.GetMouseX() >= i.ElementText.GetPosition().x && Input.GetMouseX() <= i.ElementText.GetPosition().x + i.ElementText.GetRect().GetWidth() )
				&&	( Input.GetMouseY() >= i.ElementText.GetPosition().y && Input.GetMouseY() <= i.ElementText.GetPosition().y + i.ElementText.GetRect().GetHeight() ))
			{
				i.ElementText.SetStyle(sf::String::Bold | sf::String::Italic);

				if(Input.IsMouseButtonDown(sf::Mouse::Left) && !LeftButtonDown && !i.GetType())
				{
					ChangeDirectory(WorkingPath + i.GetName() + "\\", i.GetName());
					LeftButtonDown = false;
					break;
				}

				if(Input.IsMouseButtonDown(sf::Mouse::Left) && i.GetType())
				{
					PathDragDrop = WorkingPath + i.GetName();
				}

			}
			iNb++;

			App.Draw(i.ElementText);
		}
    }

	if(Input.IsKeyDown(sf::Key::Down) && Fin < ElementList.size())
	{
		Debut++;
		Fin++;
	}

	if(Input.IsKeyDown(sf::Key::Up) && Debut > 0)
	{
		Debut--;
		Fin--;
	}

	App.Draw(LineExplorer);

	LeftButtonDown = Input.IsMouseButtonDown(sf::Mouse::Left);
}